/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2012 Gary Paluk, http://www.plugin.io
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

package io.plugin.collections 
{
	import io.plugin.collections.core.AbstractList;
	import io.plugin.collections.interfaces.ICollection;
	import io.plugin.core.interfaces.ICloneable;
	import io.plugin.collections.ns.plugin_collections;
	
	/**
	 * ...
	 * @author Gary Paluk
	 */
	public class ArrayList extends AbstractList implements ICloneable 
	{
		
		use namespace plugin_collections;
		
		private var mData: Array;
		private var mSize: int;
		
		public function ArrayList() 
		{
			mData = [];
		}
		
		override public function size():uint 
		{
			return mSize;
		}
		
		override public function isEmpty():Boolean 
		{
			return ( mSize == 0 ); 
		}
		
		override public function contains(o:Object):Boolean 
		{
			return indexOf( o ) >= 0; 
		}
		
		override public function indexOf(o:Object):int 
		{
			var i: int;
			
			if ( o == null ) {
				for ( i = 0; i < mSize; i++ )
					if ( mData[ i ] == null )
						return i;
			}
			else
			{
				for ( i = 0; i < mSize; i++ )
					if ( o.equals( mData[ i ] ))
						return i;
			}
			return -1;
		}
		
		override public function lastIndexOf(o:Object):int 
		{
			var i: int;
			if ( o == null ) {
				for ( i = mSize-1; i >= 0; i-- )
					if ( mData[ i ] == null )
						return i;
			}
			else
			{
				for ( i = mSize - 1; i >= 0; i-- )
					if ( o.equals( mData[ i ] ) )
						return i;
			}
			return -1;
		}
		
		public function clone(): ArrayList
		{
			
			var clone: ArrayList = new ArrayList();
			clone.addAll( this );
			clone.modCount = modCount;
			
			return clone;
		}
		
		override public function toArray():Array 
		{
			var ret:Array = new Array( mData.length );
			for ( var i: int = 0; i < mData.length; i++ )
			{
				ret[ i ] = mData[ i ];
			}
			
			return ret;
		}
		
		override public function getAt(index:uint):Object 
		{
			rangeCheck( index );
			return mData[ index ];
		}
		
		override public function setAt(index:uint, o:Object):Object 
		{
			rangeCheck( index );
			var oldValue: Object = mData[ index ];
			mData[ index ] = o;
			return oldValue;
		}
		
		override public function add(o:Object):Boolean 
		{
			mData[ mSize++ ] = e;
			return true; 
		}
		
		override public function addAt(index:uint, o:Object):void 
		{
			rangeCheckForAdd( index );
			mData.splice( index, 0, o );
		}
		
		override public function removeAt(index:uint):Object 
		{
			rangeCheck( index );
			
			modCount++;
			var oldValue: Object = mData[ index ];
			
			var numMoved: int = mSize - index - 1;
			
			if ( numMoved > 0 )
			{
				_modCount ++ ;
				mData.splice(id, 1);
			}
			return old ; 
		}
		
		
		override public function remove(o:Object):Boolean 
		{
			var i: int;
			if ( o == null ) {
				for ( i = 0; i < mSize; i++ )
					if ( mData[ i ] == null ) {
						removeAt( i );
						return true;
					}
					
			}
			else
			{
				for ( i = 0; i < mSize; i++ )
					if ( o.equals( mData[ i ] ) ) {
						removeAt( i )
						return true;
					}
			}
			
			return false;
		}
		
		override public function clear():void 
		{
			modCount++;
			mData = [];
			mSize = 0;
		}
		
		
		private function rangeCheck( index: int ): void
		{
			if ( index >= mSize )
				throw new RangeError();
		}
		
		
		
	}

}